package application.generateReports;

import java.sql.SQLException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import application.CommunicationModule.Communication;

public class DateReport extends Report{

	
private ArrayList<DataToDateReport> dataToReport = new ArrayList<DataToDateReport>();

	
	public DateReport(String user, String password) throws ClassNotFoundException, SQLException {
		super();
		this.user = user;
		this.password = password;
		
		this.creationDate = new  Date(calendar.get(Calendar.YEAR) - 1900,
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));
        
		this.creationTime = new Time(calendar.get(Calendar.HOUR_OF_DAY),
				calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
		
		
	
		this.reportName = "Daily report";
		Communication c = new Communication(this.user, this.password);
		
		for (int i=0; i<c.getParcelsByDate(format.format(creationDate)).size(); i++){
			this.dataToReport.add(new DataToDateReport(c.getParcelsByDate(format.format(creationDate)).get(i), c.getClientsByField("id", String.valueOf(c.getParcelsByDate(format.format(creationDate)).get(i).getRecipient())).get(0)));
			}
		
	
	}
	
	
		public DateReport(String user, String password, String from, String to) throws ClassNotFoundException, SQLException{
		
		super();
		this.user = user;
		this.password = password;
		
		this.creationDate = new  Date(calendar.get(Calendar.YEAR) - 1900,
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));
        
		this.creationTime = new Time(calendar.get(Calendar.HOUR_OF_DAY),
				calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
		
		
		
		
		this.reportName = "From " + from +" to " + to;
		Communication c = new Communication(this.user, this.password);
		
		
		String delemiter = "-";
		String[] fromDate;
		String[] toDate;
		String[] tempDate;
		
		
		
		fromDate = from.split(delemiter);
		toDate = to.split(delemiter);
		
		for(int i=0 ; i < c.getAllParcel().size(); i++) {
			tempDate = c.getAllParcel().get(i).getDate().split(delemiter);

			if((Integer.parseInt(fromDate[0]) < Integer.parseInt(tempDate[0]) && Integer.parseInt(tempDate[0]) < Integer.parseInt(toDate[0])) ||
				(Integer.parseInt(fromDate[0]) == Integer.parseInt(tempDate[0]) && Integer.parseInt(tempDate[0]) < Integer.parseInt(toDate[0])) ||
				(Integer.parseInt(tempDate[0]) == Integer.parseInt(toDate[0]) && Integer.parseInt(tempDate[1]) <= Integer.parseInt(toDate[1]) && Integer.parseInt(tempDate[2]) <= Integer.parseInt(toDate[2]))){
				
				dataToReport.add(new DataToDateReport(c.getAllParcel().get(i), c.getClientsByField("id", String.valueOf(c.getAllParcel().get(i).getRecipient())).get(0)));	
			
			}

			
		}
			
	}


	public ArrayList<DataToDateReport> getDataToReport() {
	return dataToReport;
	}


	public void setDataToReport(ArrayList<DataToDateReport> dataToReport) {
	this.dataToReport = dataToReport;
	}



	
	
}
